package com.hyacinth.forumbite.service.impl;

import com.hyacinth.forumbite.common.AppResult;
import com.hyacinth.forumbite.common.ResultCode;
import com.hyacinth.forumbite.dao.MessageMapper;
import com.hyacinth.forumbite.exception.ApplicationException;
import com.hyacinth.forumbite.model.Message;
import com.hyacinth.forumbite.model.User;
import com.hyacinth.forumbite.service.MessageService;
import com.hyacinth.forumbite.service.UserService;
import com.hyacinth.forumbite.utils.StringUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author: Yun
 * @Date: 2025/5/26
 */
@Slf4j
@Service
public class MessageServiceImpl implements MessageService {
    @Resource
    private MessageMapper messageMapper;

    @Resource
    private UserService userService;

    @Override
    public void create(Message message) {
        if (message == null || StringUtil.isEmpty(message.getContent()) ||
                message.getPostUserId() == null || message.getReceiveUserId() == null) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        User user = userService.selectById(message.getReceiveUserId());
        if (user == null || user.getDeleteState() == 1) {
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
        // 设置默认值
        Date date = new Date();
        message.setState((byte) 0)
                .setDeleteState((byte) 0)
                .setCreateTime(date)
                .setUpdateTime(date);

        int row = messageMapper.insertSelective(message);
        if (row != 1) {
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }

    @Override
    public Integer selectUnreadCount(Long id) {
        if (id == null || id <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        User user = userService.selectById(id);
        if (user == null || user.getDeleteState() == 1) {
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS));
        }
        return messageMapper.selectUnreadCount(id);
    }

    @Override
    public List<Message> selectListByUserId(Long id) {
        if (id == null || id <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        return messageMapper.selectListByUserId(id);
    }

    @Override
    public void updateStateById(Long id, Byte state) {
        if (id == null || id <= 0 || state == null || state < 0 || state > 2) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Message updateMessage = new Message()
                .setId(id)
                .setState(state)
                .setUpdateTime(new Date());
        int row = messageMapper.updateByPrimaryKeySelective(updateMessage);
        if (row != 1) {
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }

    @Override
    public Message selectById(Long id) {
        if (id == null || id <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        return messageMapper.selectByPrimaryKey(id);
    }

    @Override
    public void reply(Long messageId, Message message) {
        if (messageId == null || messageId <= 0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Message existsMessage = messageMapper.selectByPrimaryKey(messageId);
        if (existsMessage == null || existsMessage.getDeleteState() == 1) {
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
        updateStateById(messageId, (byte) 2);
        create(message);
    }
}
